export const cleanName = (name: string) => {
    if (!name) return '';
    // 名称映射，统一化
    const conceptName = name
        .replace("商业航天", "航天")
        .replace("中概互联网", "互联网")
        .replace("科技指数", "科技")
        .replace("科技HK", "科技")
        .replace("中概互联", "互联网")
        .replace("互联网科技", "互联网")
        .replace("航空航天", "航天")
        .replace("黄金股", "黄金")
        .replace("国防军工", "军工")
        .replace("金融科技", "金融")
        .replace("地产链", "地产")
        .replace("创新药企", "创新药")
        .replace("绿电", "绿色电力")
        .replace("稀有金属", "小金属")
        .replace("光伏龙头", "光伏")
        .replace("5G通信", "5G")
        .replace('互联金融', '金融')
        .replace('互联网金融', '金融')
        .replace('券商概念', '证券')
        .replace('国产芯片', '芯片')
        .replace('电力行业', '电力')
        .replace('黄金票', '黄金')
        .replace('西藏水电站', '西藏水电')
        .replace('雅下水电站', '雅下水电')
        .replace("房地产", "地产");
    return conceptName.replace(/\s|港股通|恒生|\d+ETF|龙头|港股|概念|概念股|产业|标普|纳指|科创板|创业板|科创|创业/g, '');
};


export const uniqueArray = <T extends Record<string, any>>(arr: T[], key?: keyof T): T[] => {
    if (!key) {
        return arr.filter((value, index, selfArr) =>
            index === selfArr.indexOf(value)
        )
    }

    return arr.filter((obj, index, selfArr) =>
        index === selfArr.findIndex(el => obj[key] === el[key])
    )
}

// Case-insensitive substring check using includes
function isSubstring(str1: string, str2: string): boolean {
    return str2.toLowerCase().includes(str1.toLowerCase()) || str1.toLowerCase().includes(str2.toLowerCase());
}

export const uniqueSimilarArray = <T extends Record<string, any> | string>(arr: T[], key?: keyof T): T[] => {
    if (!key) {
        return arr.filter((str, index) => {
            for (let i = 0; i < index; i++) {
                if (isSubstring(str as string, arr[i] as string)) {
                    return false
                }
            }
            return true;
        })
    }
    return arr.filter((o, index) => {
        for (let i = 0; i < index; i++) {
            if (isSubstring(o[key] as string, arr[i][key] as string)) {
                return false
            }
        }
        return true;
    })
}